package org.atf.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.atf.dp.client.DPHttpsClient;
import org.atf.model.dao.ProfileDAO;
import org.atf.model.entity.Profile;

public class DpDashboardController {
	private ProfileDAO profileDAO = new ProfileDAO();
	private  static Logger  logger = Logger.getLogger(DpDashboardController.class);
	public HashMap<String,Long> getDPSystemUsage(String region, String profileName ){
		logger.debug("region: "+region+" profileName: "+profileName);
		HashMap<String, Long> result = new HashMap<String,Long>();
		Profile dpProfile= profileDAO.getProfileByRegionName(region, profileName);
		DPHttpsClient httpsCleintObj;
		try {
			logger.debug("Calling "+dpProfile.getHostname());
			httpsCleintObj = new DPHttpsClient(dpProfile.getHostname(), dpProfile.getPort(), dpProfile.getUsername(), dpProfile.getPassword());
			httpsCleintObj.connect(true);		
			HashMap<String, String> temp=httpsCleintObj.getSystemUsage();
			logger.debug("temp: "+temp);
			httpsCleintObj.releaseConnection();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	        java.util.Date date = sdf.parse((String)temp.get("timestamp"));
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	    	result.put("timestamp", cal.getTimeInMillis());
			result.put("Interval", Long.valueOf(temp.get("Interval")));
			result.put("Load", Long.valueOf(temp.get("Load")));
			result.put("WorkList", Long.valueOf(temp.get("WorkList")));
			logger.debug("result "+result);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
	    
		return result;
	}
	
	public HashMap<String, Long> getCPUUsage(String region, String profileName ){
		logger.debug("region: "+region+" profileName: "+profileName);
		HashMap<String, Long> result = new HashMap<String,Long>();
		Profile dpProfile= profileDAO.getProfileByRegionName(region, profileName);
		DPHttpsClient httpsCleintObj;
		try {
			logger.debug("Calling "+dpProfile.getHostname());
			httpsCleintObj = new DPHttpsClient(dpProfile.getHostname(), dpProfile.getPort(), dpProfile.getUsername(), dpProfile.getPassword());
			httpsCleintObj.connect(true);		
			HashMap<String, String> temp=httpsCleintObj.getCPUUsage();
			logger.debug("temp: "+temp);
			httpsCleintObj.releaseConnection();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	        java.util.Date date = sdf.parse((String)temp.get("timestamp"));
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	    	result.put("timestamp", cal.getTimeInMillis());
			result.put("OneMinute", Long.valueOf(temp.get("oneMinute")));
			result.put("TenSeconds", Long.valueOf(temp.get("tenSeconds")));
			result.put("TenMinutes", Long.valueOf(temp.get("tenMinutes")));
			logger.debug("result "+result);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
		return result;
	}
	
	public List<HashMap<String, String>> getHTTPTransactionsTimes(String region, String profileName,String domainName){
		logger.debug("Domain name: "+domainName+" region: "+region+" profileName: "+profileName);
		List<HashMap<String, String>> result= new ArrayList<HashMap<String, String>>();
		Profile dpProfile= profileDAO.getProfileByRegionName(region, profileName);
		DPHttpsClient httpsCleintObj;
		try {
			logger.debug("Calling "+dpProfile.getHostname());
			httpsCleintObj = new DPHttpsClient(dpProfile.getHostname(), dpProfile.getPort(), dpProfile.getUsername(), dpProfile.getPassword());
			httpsCleintObj.connect(true);		
			result =httpsCleintObj.getHTTPTransactionsTimes(domainName);
			logger.debug("result: "+result);
		}catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
		return result;
		
	}
	
	public List<String> getDomainStatus(String region, String profileName){
		logger.debug("region: "+region+" profileName: "+profileName);
		List<String> result= new ArrayList<String>();
		Profile dpProfile= profileDAO.getProfileByRegionName(region, profileName);
		DPHttpsClient httpsCleintObj;
		try {
			logger.debug("Calling "+dpProfile.getHostname());
			httpsCleintObj = new DPHttpsClient(dpProfile.getHostname(), dpProfile.getPort(), dpProfile.getUsername(), dpProfile.getPassword());
			httpsCleintObj.connect(true);		
			result =httpsCleintObj.getDomainStatus();
			logger.debug("result: "+result);
		}catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
		return result;
		
	}
	public HashMap<String,Long> getMemoryStatus(String region, String profileName ){
		logger.debug("region: "+region+" profileName: "+profileName);
		HashMap<String, Long> result = new HashMap<String,Long>();
		Profile dpProfile= profileDAO.getProfileByRegionName(region, profileName);
		DPHttpsClient httpsCleintObj;
		try {
			logger.debug("Calling "+dpProfile.getHostname());
			httpsCleintObj = new DPHttpsClient(dpProfile.getHostname(), dpProfile.getPort(), dpProfile.getUsername(), dpProfile.getPassword());
			httpsCleintObj.connect(true);		
			HashMap<String, String> temp=httpsCleintObj.getMemoryStatus();
			logger.debug("temp: "+temp);
			httpsCleintObj.releaseConnection();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	        java.util.Date date = sdf.parse((String)temp.get("timestamp"));
	        Calendar cal = Calendar.getInstance();
	        cal.setTime(date);
	    	result.put("timestamp", cal.getTimeInMillis());
			result.put("Usage", Long.valueOf(temp.get("Usage")));
			result.put("TotalMemory",  Long.valueOf(temp.get("TotalMemory"))/1024L);
			result.put("UsedMemory", Long.valueOf(temp.get("UsedMemory"))/1024L);
			result.put("FreeMemory", Long.valueOf(temp.get("FreeMemory"))/1024L);
			result.put("ReqMemory", Long.valueOf(temp.get("ReqMemory"))/1024L);
			result.put("HoldMemory", Long.valueOf(temp.get("HoldMemory"))/1024L);
			result.put("ReservedMemory", Long.valueOf(temp.get("ReservedMemory"))/1024L);
			result.put("InstalledMemory", Long.valueOf(temp.get("InstalledMemory"))/1024L);
			
			
			
			logger.debug("result "+result);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
	    
		return result;
	}
	
	public HashMap<String,HashMap<String,Long>> getAllDatapowerData(String region, String profileName){
		 HashMap<String,HashMap<String,Long>> result=new HashMap<String,HashMap<String,Long>> ();
		 logger.debug("region: "+region+" profileName: "+profileName);
		 Profile dpProfile= profileDAO.getProfileByRegionName(region, profileName);
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		 Calendar cal = Calendar.getInstance();
		 DPHttpsClient httpsCleintObj;
		 try {
				logger.debug("Calling "+dpProfile.getHostname());
				httpsCleintObj = new DPHttpsClient(dpProfile.getHostname(), dpProfile.getPort(), dpProfile.getUsername(), dpProfile.getPassword());
				httpsCleintObj.connect(true);		
				HashMap<String, String> dpStatus=httpsCleintObj.getDPStatus();	
				httpsCleintObj.releaseConnection();
	
				java.util.Date date = sdf.parse((String)dpStatus.get("timestamp"));
				//Memory
		        HashMap<String,Long> mResult=new  HashMap<String,Long>();
		        mResult.put("timestamp", cal.getTimeInMillis());
		        mResult.put("Usage", Long.valueOf(dpStatus.get("Usage")));
		        mResult.put("TotalMemory",  Long.valueOf(dpStatus.get("TotalMemory"))/1024L);
		        mResult.put("UsedMemory", Long.valueOf(dpStatus.get("UsedMemory"))/1024L);
		        mResult.put("FreeMemory", Long.valueOf(dpStatus.get("FreeMemory"))/1024L);
		        mResult.put("ReqMemory", Long.valueOf(dpStatus.get("ReqMemory"))/1024L);
		        mResult.put("HoldMemory", Long.valueOf(dpStatus.get("HoldMemory"))/1024L);
		        mResult.put("ReservedMemory", Long.valueOf(dpStatus.get("ReservedMemory"))/1024L);
		        mResult.put("InstalledMemory", Long.valueOf(dpStatus.get("InstalledMemory"))/1024L);
				logger.trace("mResult "+mResult);
				result.put("MemoryStatus", mResult);
				//CPU
			    HashMap<String,Long> cResult=new  HashMap<String,Long>();
			    cResult.put("timestamp", cal.getTimeInMillis());
			    cResult.put("OneMinute", Long.valueOf(dpStatus.get("oneMinute")));
				cResult.put("TenSeconds", Long.valueOf(dpStatus.get("tenSeconds")));
				cResult.put("TenMinutes", Long.valueOf(dpStatus.get("tenMinutes")));
				logger.trace("cResult "+cResult);
				result.put("CPUUsage", cResult);
		
				//System Load
		        HashMap<String,Long> sResult=new  HashMap<String,Long>();
		        sResult.put("timestamp", cal.getTimeInMillis());
		        sResult.put("Interval", Long.valueOf(dpStatus.get("Interval")));
		        sResult.put("Load", Long.valueOf(dpStatus.get("Load")));
		        sResult.put("WorkList", Long.valueOf(dpStatus.get("WorkList")));
				logger.trace("sResult "+sResult);
				result.put("SystemUsage", sResult);
			
				logger.trace("result "+result);	
			} catch (Exception e) {
				// TODO Auto-generated catch block
				logger.error(e);
			}
		 return result;
	}
	
	public HashMap<String,String> getFilesystemStatus(String region, String profileName){
		HashMap<String, String> result = new HashMap<String,String>();
		logger.debug("region: "+region+" profileName: "+profileName);
		Profile dpProfile= profileDAO.getProfileByRegionName(region, profileName);
		DPHttpsClient httpsCleintObj;
		try {
			logger.debug("Calling "+dpProfile.getHostname());
			httpsCleintObj = new DPHttpsClient(dpProfile.getHostname(), dpProfile.getPort(), dpProfile.getUsername(), dpProfile.getPassword());
			httpsCleintObj.connect(true);		
			result=httpsCleintObj.getFilesystemStatus();
			httpsCleintObj.releaseConnection();
			
			logger.debug("result "+result);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
	    
		return result;
	}
}
